/**
 * Copyright (C) 2007 Mustafa K. Isik - isik@acm.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package com.paxcodi.sockets.rpc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SimpleClient {

    /**
     * @param args host.domain port string OR host.domain port number1 number2
     */
    public static void main(String[] args) {
	//check for correct number of parameters
	if(args.length < 3 || args.length > 4){
	    System.err.println("usage:\n" + 
		    "java SimpleClient <host> <port> <integer_number1> " + 
		    "<integer_number2>\n" + "OR\njava SimpleClient " + 
	    "<host> <port> <string>\n");
	    return;
	}
	String serverName = args[0];
	int serverPort = Integer.parseInt(args[1]);
	//construct socket
	Socket remoteServer = null;
	try {

	    remoteServer = new Socket(serverName,serverPort);
	    //get writers and readers for the socket

	    OutputStream routeToRemote = 
		new BufferedOutputStream(remoteServer.getOutputStream());
	    OutputStreamWriter writeToRemote = 
		new OutputStreamWriter(routeToRemote);

	    InputStream routeFromRemote = 
		new BufferedInputStream(remoteServer.getInputStream());//in bytes
	    InputStreamReader unbufferedReadFromRemote = 
		new InputStreamReader(routeFromRemote); //in characters, unbuff.
	    BufferedReader readFromRemote = 
		new BufferedReader(unbufferedReadFromRemote); //in chars, buff.

	    String msg = null;
	    if(args.length == 3) {
		//perform string capitalization or shutdown
		msg = args[2] +"\n";
	    } else {
		//there is only one possibility for the #args, that is to be 4, 
		//i.e. perform addition
		int firstNumber = Integer.parseInt(args[2]); //interpret args[2]
		int secondNumber = Integer.parseInt(args[3]); //analogous
		msg = "ADD\n"+firstNumber+"\n"+secondNumber+"\n";
	    }

	    //send msg to remote service for processing
	    System.out.println("Sending command:\n" + msg);
	    writeToRemote.write(msg);
	    writeToRemote.flush();

	    //receive result and print out on standard output
	    String readResult = readFromRemote.readLine();
	    if(!(readResult == null))
		System.out.println("result: " + readResult);

	} catch (UnknownHostException e) {
	    System.err.println("Cannnot find host: " + serverName + "\n" + e);
	} catch (SocketException e) {
	    System.err.println("Cannot connect to host: " + 
		    serverName + "\n" + e);
	} catch (IOException e) {
	    System.err.println(e);
	} finally {

	    try {
		if (remoteServer != null) 
		    remoteServer.close();
	    } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }

	}//finally

    }

}
